<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Cookbook_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 根据学校，日期和类型得到菜谱信息
	 */
	public function find_by_date_type($schoolid,$date,$type){
		$this->db->select('t_cookbook.id,t_cookbook.daylim,t_product.name');
		$this->db->from('t_cookbook');
		$this->db->join('t_product', 't_cookbook.productid = t_product.id','left');
		$this->db->where('schoolid',$schoolid);
		$this->db->where('date',$date);
		$this->db->where('t_cookbook.type',$type);
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 根据学校，日期和类型得到菜谱信息
	 */
	public function find_product_by_date_type($schoolid,$date,$type){
		$this->db->where('schoolid',$schoolid);
		$this->db->where('date',$date);
		$this->db->where('type',$type);
		$query = $this->db->get('t_cookbook');
		return $query->result_array();
	}
	/*
	 * 根据学校，日期和类型返回菜谱数量(导出学生订餐表)(新)
	 */
	public function count_by_school_date_type($schoolid,$date,$type){
		$this->db->where('schoolid',$schoolid);
		$this->db->where('date',$date);
		if($type!=-1){
			$this->db->where('type',$type);
		}
		$num = $this->db->count_all_results('t_cookbook');
		return $num;
	}
	/*
	 * 根据学校，日期，商品ID和类型得到菜谱信息(判断是否重复添加)
	 */
	public function find_by_date_product_type($schoolid,$date,$productid,$type){
		$this->db->where('schoolid',$schoolid);
		$this->db->where('date',$date);
		$this->db->where('productid',$productid);
		$this->db->where('type',$type);
		$query = $this->db->get('t_cookbook');
		return $query->row_array();
	}
	/*
	 * 保存菜谱信息
	 */
	public function create($cookbook){
		$this->db->insert('t_cookbook', $cookbook);
		return $this->db->insert_id();
	}
	/*
	 * 更新菜谱信息
	 */
	public function update($id,$cookbook){
		$this->db->where('id', $id);
		$this->db->update('t_cookbook', $cookbook);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 删除菜谱信息
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_cookbook');
		return $this->db->affected_rows()>0;
	}
	/*
	 * 返回菜谱信息
	 */
	public function find_by_id($id){
		$this->db->where('id>=', $id);
		$query = $this->db->get('t_cookbook');
		return $query->result_array();
	}
}